package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.annotation.UserAnnotation;
import cn.wolfcode.luowowo.domain.StrategyComment;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.query.StrategyQuery;
import cn.wolfcode.luowowo.redis.Service.IStatisVoRedisService;
import cn.wolfcode.luowowo.redis.Service.IUserInfoRedisService;
import cn.wolfcode.luowowo.service.IStrategyCommentService;
import cn.wolfcode.luowowo.service.IStrategyService;
import cn.wolfcode.luowowo.service.IStrategyThemeService;
import cn.wolfcode.luowowo.util.JsonResult;
import cn.wolfcode.luowowo.vo.StatisVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("strategies")
public class StrategyController {


    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyThemeService strategyThemeService;
    @Autowired
    private IStrategyCommentService strategyCommentService;
    @Autowired
    private IUserInfoRedisService redisService;
    @Autowired
    private IStatisVoRedisService statisVoRedisService;


    @GetMapping("detail")
    public Object detail(String id) {
        //根据id查询攻略明细
        //如果刷新,需要将阅读数+1
        statisVoRedisService.increateViewnum(id);
        return JsonResult.success(strategyService.selectOne(id));
    }

    @GetMapping("query")
    public Object query(StrategyQuery qo) {
        //分页
        return JsonResult.success(strategyService.query(qo));
    }


    @GetMapping("comments")
    public Object comments(StrategyCommentQuery qo) {
        //分页
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    @GetMapping("themes")
    public Object themes() {
        //分页
        return JsonResult.success(strategyThemeService.list());
    }

    @RequireLogin
    @PostMapping("addComment")
    public Object addComment(StrategyComment strategyComment, @UserAnnotation UserInfo userInfo) {
        //获取当前用户信息
        //将当前用户信息加入评论中
        BeanUtils.copyProperties(userInfo, strategyComment);
        //id得额外加
        strategyComment.setUserId(userInfo.getId());
        strategyCommentService.addComment(strategyComment);
        //获取攻略明细id
        String strategyId = strategyComment.getStrategyId();
        //点击添加评论,评论数要+1
        statisVoRedisService.increateReplynum(strategyId);
        return JsonResult.success();
    }


    @RequireLogin
    @PostMapping("commentThumb")
    public Object commentThumb(String cid, @UserAnnotation UserInfo userInfo) {
        //当前用户id
        String uid = userInfo.getId();
        System.out.println(uid);
        //点赞
        strategyCommentService.commentThumb(cid, uid);
        return JsonResult.success();
    }


    @GetMapping("statisVo")
    public Object statisVo(String sid){
        //根据sid获取统计数据信息
        StatisVo statisVos = statisVoRedisService.getStatisVo(sid);
        return JsonResult.success(statisVos);
    }


    @RequireLogin
    @PostMapping("favor")
    public Object favor(String sid,@UserAnnotation UserInfo userInfo) {
        String uid = userInfo.getId();
        //收藏处理
        Boolean booledan = statisVoRedisService.favornum(sid,uid);
        return JsonResult.success(booledan);
    }

    @RequireLogin
    @PostMapping("strategyThumbup")
    public Object strategyThumbup(String sid,@UserAnnotation UserInfo userInfo) {
        //顶处理
        String uid = userInfo.getId();
        Boolean b = statisVoRedisService.strategyThumbup(sid , uid);
        return JsonResult.success(b);
    }

}
